#include <stdio.h>
#include <stdint.h>

int main(int argc, char* argv[]) {
  printf("Enter mac address. Format: 0211:22ff:fe33:4455\n");
  uint8_t bytes[8];
  char filename[255];
  scanf("%2hhx%2hhx:%2hhx%2hhx:%2hhx%2hhx:%2hhx%2hhx", bytes, bytes+1, bytes+2, bytes+3, bytes+4, bytes+5, bytes+6, bytes+7);
  printf("Enter filename\n");
  scanf("%255s", filename);
  uint8_t crc = 0x08; // init to the length of the line
  int i = 0;
  for(i=0; i<8; ++i) {
    crc += bytes[i];
  }
  crc = (0x100 - crc);
  FILE * fp = fopen(filename, "w");
  if( fp != NULL) {
    fprintf(fp, ":08000000%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7], crc);
    fprintf(fp, ":00000001FF\n");
    fclose(fp);
    return 0;
  } else {
    printf("Could not open file\n");
    return -1;
  }
}

